package com.vancouver.knows.service;

import android.app.PendingIntent;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.MainActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.my.MyAtcommentActivity;
import com.vancouver.knows.ui.activity.my.MyUserPostCommentsActivity;
import com.vancouver.knows.util.AppUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by llf on 2017/4/12.
 */

public class MessagingService extends FirebaseMessagingService {

    public static final String TAG = MessagingService.class.getSimpleName();
    public static final String NOTIFICATION_TYPE = "notification_type";

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i(TAG, "Message received");
        Log.d(TAG, "From: " + remoteMessage.getFrom());  //topics/broadcast-android

        // Check if message contains a data payload.
        Map<String,String> remoteMessageMap = new HashMap<>();
        String notificationValue = null;
        String postId = null;
        remoteMessageMap = remoteMessage.getData();
        if (remoteMessageMap.size() > 0) {

            try {
                notificationValue = remoteMessage.getData().get("notification_type");
                postId = remoteMessage.getData().get("post_id");
                Log.d(TAG, "Message data payload: " + notificationValue);
                // Check if message contains a notification payload.
                RemoteMessage.Notification notification = remoteMessage.getNotification();
                String messageBody = null;
                if (notification != null && notificationValue != null) {
                    messageBody = notification.getBody();
                    Log.d(TAG, "Message Notification Body: " + messageBody);

                }

                setNotification(messageBody, notificationValue, postId);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    public void setNotification(String message, String pushValue, String postId) {
        Log.e(TAG, "No subscribers!");
        if (message == null) return;
        MainApplication imContext = (MainApplication) getApplication();

        Intent intent = new Intent();
        if ("breaking_news".equals(pushValue)) {
            intent.setClass(this, CityNewsDetailActivity.class);
            intent.putExtra("postId", postId);

        } else if ("new_comment".equals(pushValue)) {
            if (AppUtils.getLoginStatus(imContext)) {
                intent.setClass(this, MyUserPostCommentsActivity.class);
            }

        } else if ("new_reply".equals(pushValue)) {
            if (AppUtils.getLoginStatus(imContext)) {
                intent.setClass(this, MyAtcommentActivity.class);
            }

        } else {
            //如果不符合，则调到主页或者登录页
            if (AppUtils.getLoginStatus(imContext)) {
                intent.setClass(this, MainActivity.class);
            }
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        String title = getString(R.string.app_name);

        NotificationCompat.BigTextStyle contentText = new NotificationCompat.BigTextStyle();
        contentText.bigText(message);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notication_icon)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(contentText)
                .setVibrate(new long[]{1000, 1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setAutoCancel(true);

        //Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(NOTIFICATION_TYPE, true);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        Random random = new Random();
        int notificationId = random.nextInt(9999 - 1000) + 1000;
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId, builder.build());
    }
}
